import { RequestHandler } from 'express'
import { failure, success } from '@/utils/response'
import logService from '@/services/logService'

const getLogs: RequestHandler = async (req, res) => {
  try {
    const data = await logService.getLogs(req.query)
    success(res, '查询日志列表成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询日志列表失败：')
  }
}

const getLogInfo: RequestHandler = async (req, res) => {
  try {
    const data = await logService.getLogInfo(+req.params.id)
    success(res, '查询日志成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询日志失败：')
  }
}

const clearLogs: RequestHandler = async (req, res) => {
  try {
    await logService.clearLogs()
    success(res, '清空日志成功。')
  } catch (error) {
    failure(res, error as Error, '清空日志失败：')
  }
}

const deleteLog: RequestHandler = async (req, res) => {
  try {
    await logService.deleteLog(+req.params.id)
    success(res, '删除日志成功。')
  } catch (error) {
    failure(res, error as Error, '删除日志失败：')
  }
}

export default {
  getLogs,
  getLogInfo,
  clearLogs,
  deleteLog,
}
